import Dao.DataBase as datebaseManager


class UserManager:
    """
    用户账号管理系统
    """
    State = None
    def __init__(self):
        self.db = datebaseManager.sqlite_connect("UserInfo.db3")

    @staticmethod
    def logout():
        """
        注销（退出登录)操作
        @return:
        """
        UserManager.State = None

    def vildate(self, username, password):
        """
        验证用户是否正确
        :param username: 用户名
        :param password: 密码
        :return:
        """
        ret = self.db.execute_DQL(f"select 用户名,密码 from UserInfo where 用户名 = '{username}' and 密码 = '{password}'")
        return False if ret == [] else True

    def isCreated(self, username):
        """
        用户是否已经被建立
        :return:
        """
        ret = self.db.execute_DQL(f"select 用户名,密码 from UserInfo where 用户名 = '{username}'")
        return True if ret != [] else False

    def RegUser(self, username, password):
        isCreated = self.isCreated(username)
        if isCreated:
            return (False, "已创建该账号!")
        try:
            self.db.execute_DML(f"insert into UserInfo values('{username}','{password}',0,0,0,'default.png')")
        except Exception as e:
            print(e)

        else:
            return (True, "成功创建")


if __name__ == '__main__':
    UM = UserManager()
    print(UM.vildate("", 'test'))
    print(UM.vildate("test", "test"))
    print(UM.isCreated("test12"))
    UM.RegUser("123", '123')
